package com.jcfk.eam.service.main;

import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.dao.main.EamInspectionTaskDao;
import com.jcfk.eam.domain.dto.base.EamInspectionRouteLocationDTO;
import com.jcfk.eam.domain.dto.main.EamInspectionTaskDTO;
import com.jcfk.eam.domain.dto.main.EamTaskPlanAttachDTO;
import com.jcfk.eam.domain.po.main.EamInspectionTask;
import com.jcfk.eam.domain.po.main.EamTaskPlanAttach;
import com.jcfk.eam.domain.query.base.EamInspectionRouteLocationQuery;
import com.jcfk.eam.domain.query.main.EamInspectionTaskQuery;
import com.jcfk.eam.service.base.EamInspectionRouteLocationService;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.jcfk.common.core.utils.IdUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 巡检任务
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-12-04
 */
@Service
public class EamInspectionTaskService extends BaseService<EamInspectionTaskDao, EamInspectionTask> {

    @Autowired
    private EamInspectionRouteLocationService inspectionRouteLocationService;

    @Autowired
    private EamTaskPlanAttachService eamTaskPlanAttachService;

    /**
     * EamInspectionTask分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamInspectionTaskDTO> queryByPage(DataPage<EamInspectionTaskDTO> page, EamInspectionTaskQuery query) {
        if(StrUtil.isNotEmpty(query.getTaskName())){
            query.setTaskName("%"+query.getTaskName()+"%");
        }
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamInspectionTask是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamInspectionTaskDTO dto) {
        QueryWrapper<EamInspectionTask> queryWrapper = new QueryWrapper<>();

        return RetResult.ok();
    }

    /**
     * 插入巡检任务
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamInspectionTaskDTO dto) {

        // 保存
        EamInspectionTask item = new EamInspectionTask();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新巡检任务
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamInspectionTaskDTO dto) {

        // 保存
        EamInspectionTask item = new EamInspectionTask();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }


    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamInspectionTask item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取巡检任务信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamInspectionTaskDTO get(String id) {
        // 查询数据
        EamInspectionTask item = this.baseMapper.selectById(id);
        EamInspectionTaskDTO dto = new EamInspectionTaskDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }

    /**
     * 获取检验任务查看信息
     * @param id
     * @return
     */
    public EamInspectionTaskDTO getView(String id){
        EamInspectionTaskQuery query=new EamInspectionTaskQuery();
        query.setId(id);
        EamInspectionTaskDTO inspectionTask=this.baseMapper.getView(query);
        if(StrUtil.isNotEmpty(inspectionTask.getInspectionRouteId()))
        {
            EamInspectionRouteLocationQuery routeLocationQuery=new EamInspectionRouteLocationQuery();
            routeLocationQuery.setRouteId(inspectionTask.getInspectionRouteId());
            List<EamInspectionRouteLocationDTO> locationList=inspectionRouteLocationService.getList(routeLocationQuery);
            inspectionTask.setRouteLocationList(locationList);
        }

        QueryWrapper<EamTaskPlanAttach> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EamTaskPlanAttach::getTaskPlanId, inspectionTask.getInspectionPlanId());
        List<EamTaskPlanAttach> fileList = eamTaskPlanAttachService.list(queryWrapper);
        inspectionTask.setAssetPhotos(convertList(fileList, EamTaskPlanAttachDTO.class));


        return inspectionTask;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamInspectionTaskDTO>
     */
    public List<EamInspectionTaskDTO> getList(EamInspectionTaskQuery query) {
        // 查询条件
        QueryWrapper<EamInspectionTask> qw = new QueryWrapper<>();
        qw.lambda().eq(query.getAssetKind()!=null, EamInspectionTask::getAssetKind, query.getAssetKind());
        qw.lambda().eq(!StrUtil.isEmpty(query.getInspectionPlanId()), EamInspectionTask::getInspectionPlanId, query.getInspectionPlanId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getTaskNo()), EamInspectionTask::getTaskNo, query.getTaskNo());
        qw.lambda().eq(!StrUtil.isEmpty(query.getTaskName()), EamInspectionTask::getTaskName, query.getTaskName());
        qw.lambda().eq(query.getTaskStatus()!=null, EamInspectionTask::getTaskStatus, query.getTaskStatus());
        qw.lambda().ge(!StrUtil.isEmpty(query.getPlanExecuteTimeStart()), EamInspectionTask::getPlanExecuteTime, query.getPlanExecuteTimeStart());
        qw.lambda().le(!StrUtil.isEmpty(query.getPlanExecuteTimeEnd()), EamInspectionTask::getPlanExecuteTime, query.getPlanExecuteTimeEnd());
        // 查询数据
        List<EamInspectionTask> list = super.list(qw);

        return convertList(list, EamInspectionTaskDTO.class);
    }

    /**
     * 根据主键获取巡检任务信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        this.removeById(id);
    }
}

